/*
**	Iframe-AJAX v1.0 beta
**	http://code.google.com/p/iframe-ajax/
**	Licensed under a GNU GPL version 3 by Alexandre Vicente <xandy@xandy.me>
*/
iframeajax = {};
(function($){
	// Internal engine
	$.engine = {};
	// uniqid from php.js => http://bit.ly/iLrfGN (The version here was compressed by Closure Compiler)
	$.engine.uniqid = function(c,e){typeof c=="undefined"&&(c="");var a,d=function(b,a){b=parseInt(b,10).toString(16);if(a<b.length)return b.slice(b.length-a);if(a>b.length)return Array(1+(a-b.length)).join("0")+b;return b};if(!this.php_js)this.php_js={};if(!this.php_js.uniqidSeed)this.php_js.uniqidSeed=Math.floor(Math.random()*123456789);this.php_js.uniqidSeed++;a=c;a+=d(parseInt((new Date).getTime()/1E3,10),8);a+=d(this.php_js.uniqidSeed,5);e&&(a+=(Math.random()*10).toFixed(8).toString());return a};
	// form creator
	$.engine.formalize = function(data,method){
		    if(method===undefined) method="get";
			var form = document.createElement("form");
			form.id = $.engine.uniqid("iframeajax_emulator_");
			form.method = method;
			form.style.display = "none";
			document.body.appendChild(form);
			var formy = document.getElementById(form.id);
			for(x in data){
				var input = document.createElement("input");
				input.type="hidden";
				input.name = x;
				input.id = x;
				input.value = data[x];
				formy.appendChild(input);
			}
			return formy;
	};
	// data loader
	$.ajax = function(x){
		var url = x.url;
		var callback = x.callback;
		if(x.form != undefined) var form = x.form;
		else if(x.data != undefined) var form = $.engine.formalize(x.data,x.method);
		var iframe = document.createElement('iframe');
		ifrid = $.engine.uniqid("iframeajax_emulator_");
		iframe.name=ifrid;
		form.target=ifrid;
		form.action=url;
		iframe.width="0";
		iframe.height="0";
		iframe.style.display = "none";
		iframe.id = ifrid;
        var func = function() { 
			iframeObject = document.getElementById(ifrid);
			var doc = iframeObject.contentDocument
			var serializer = new XMLSerializer();
			var content = serializer.serializeToString(doc);
			callback(content);
		};
        if(iframe.addEventListener)
            iframe.addEventListener('load', func, true);
        else if(iframe.attachEvent)
            iframe.attachEvent('onload',func);
        document.body.appendChild(iframe);
		form.submit();
	}
})(iframeajax);
